jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.providesConsumesServices.ProvidesConsumesServices", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.interfaceSelectedSubject = "com.tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces.interfaceSelected";
        	
        	plugin.interfaceConsumersTableCacheId = "SPM_INTERFACE_CONSUMERS_TABLE_CACHE";
        	plugin.referenceProvidersTableCacheId = "SPM_REFERENCE_PROVIDERS_TABLE_CACHE";
        	
        	plugin.prototype.isInterfaceConsumersMode;
        	plugin.prototype.nameSuffix = "";
        	
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function(nameSuffix, isInterfaceConsumersMode) {
				this.nameSuffix = nameSuffix;
				var servicesTitle = this.getDescendantOfName("titleLabel");
				servicesTitle.setName(servicesTitle.getName() + nameSuffix);
                var servicesTable = this.getDescendantOfName("servicesTable");
				servicesTable.setName(servicesTable.getName() + nameSuffix);
				
            	this.isInterfaceConsumersMode = isInterfaceConsumersMode;
            	
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(plugin.interfaceSelectedSubject, this.interfaceSelected, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.page.serviceView.tabContent.providesConsumes.ProvidesConsumes.handleInitialSelectSubject, this.handleInitialSelect, this);
			}
			
			plugin.prototype.handleInitialSelect = function(subject, message, providesConsumesServicesObj) {
			   plugin.clearCache(providesConsumesServicesObj);
			}

			plugin.clearCache = function(providesConsumesServicesObj) {
			   var cacheId;
			   var secondaryTableCacheId;
			   if(providesConsumesServicesObj.isInterfaceConsumersMode)
   			   { 
			      cacheId = plugin.interfaceConsumersTableCacheId; 
			      secondaryTableCacheId = tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces.serviceInterfacesTableCacheId;
			   }
   			   else
   			   { 
			      cacheId = plugin.referenceProvidersTableCacheId;
			      secondaryTableCacheId = tibco.admin.monitormanage.uicomponents.table.providesConsumesInterfaces.ProvidesConsumesInterfaces.interfaceReferencesTableCacheId;
			   }
			   var server = tibco.admin.Main.getServer();
   			   server.Cache.clearById(cacheId, true);
			   server.Cache.clearById(secondaryTableCacheId, true);
			}
			
			plugin.prototype.interfaceSelected = function(subject, message, providesConsumesServicesObj) {
				var interfaceId = message.interfaceId;
				if(interfaceId)
				{
					// no data cache found so make the webservice call
					var isServiceInterfaceMode = message.isServiceInterfaceMode;
					if(isServiceInterfaceMode == providesConsumesServicesObj.isInterfaceConsumersMode)
					{ providesConsumesServicesObj.callGetProvidesConsumesServices(interfaceId, providesConsumesServicesObj); }
				}
				else
				{
					// no interface selected so clear the table data
				        plugin.clearCache(providesConsumesServicesObj);
   					
   					var tableObj = providesConsumesServicesObj.getDescendantOfName("servicesTable" + providesConsumesServicesObj.nameSuffix);
		      		tableObj.repaintData();
				}
			}
			
			plugin.showProvidesConsumesServiceDetails = function(serviceKey, consumerProviderKeyVal, isInterfaceConsumersModeVal) {
				var paramArray = { serviceId:serviceKey, consumerProviderKey:consumerProviderKeyVal, isInterfaceConsumersMode:isInterfaceConsumersModeVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId, paramArray); 
			}
			
			//call this method to begin the service call (eg.service.callGetProvidesConsumesServices();)
    		plugin.prototype.callGetProvidesConsumesServices = function(interfaceId, providesConsumesServicesObj) {
    			var rulesPath;
    			var dataPath;
    			var operation;
    			if(providesConsumesServicesObj.isInterfaceConsumersMode)
    			{ 
    				rulesPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/rules/interfaceConsumersRules.xml"; 
    				dataPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/data/interfaceConsumersData.xml"; 
    				operation = "getInterfaceConsumers";
    			}
    			else
    			{ 
    				rulesPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/rules/referenceProvidersRules.xml"; 
    				dataPath = "tibco/admin/monitormanage/uicomponents/table/providesConsumesServices/data/referenceProvidersData.xml"; 
    				operation = "getReferenceProviders";
    			}

    			if (tibco.admin.Main.getServer().getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(providesConsumesServicesObj.getNamespace(), rulesPath, operation);
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL(dataPath);
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(providesConsumesServicesObj.getNamespace(), rulesPath), operation, null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(providesConsumesServicesObj.getNamespace(), dataPath));
                }
                objService.providesConsumesServicesObj = providesConsumesServicesObj;
                objService.interfaceKey = interfaceId;
                objService.timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId();
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("type");
				metricsList.add("faults");
				metricsList.add("successes");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, providesConsumesServicesObj.ongetProvidesConsumesServicesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, providesConsumesServicesObj.ongetProvidesConsumesServicesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, providesConsumesServicesObj.ongetProvidesConsumesServicesInvalid);
      			objService.doCall();
   	 		};

    		plugin.prototype.ongetProvidesConsumesServicesSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			
      			var providesConsumesServicesObj = objEvent.target.providesConsumesServicesObj;
      			
      			// update the cache with the proper attributes for the table to render
      			var cacheId;
      			if(providesConsumesServicesObj.isInterfaceConsumersMode)
      			{ cacheId = plugin.interfaceConsumersTableCacheId; }
      			else
      			{ cacheId = plugin.referenceProvidersTableCacheId; }
      			var cacheDoc = tibco.admin.Main.getServer().Cache.getDocument(cacheId);
      			var records = cacheDoc.getChildNodes();
      			for(i = 0; i < records.size(); i++)
      			{
      				var record = records.get(i);
      				var faults = record.getAttribute("faults");
      				var successes = record.getAttribute("successes");
      				var faultSuccessRatio = faults + "/" + successes;
      				var faultSuccessRatioValue = faults/successes;
      				record.setAttribute("faultSuccessRatio",faultSuccessRatio);
      				record.setAttribute("faultSuccessRatioValue",faultSuccessRatioValue);
      				var hits = (faults - 0) + (successes - 0);
      				record.setAttribute("hits", hits);
      				
      				record.setAttribute("isInterfaceConsumersMode", providesConsumesServicesObj.isInterfaceConsumersMode);
      			}
      			
      			var tableObj = providesConsumesServicesObj.getDescendantOfName("servicesTable" + providesConsumesServicesObj.nameSuffix);
      			tableObj.setXMLId(cacheId);
      			tableObj.repaintData();
    		};

    		plugin.prototype.ongetProvidesConsumesServicesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			//jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetProvidesConsumesServicesInvalid = function(objEvent) {
      			//jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};
            
        });